<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Teacher extends CI_Controller{
    /**
     * 学生分数添加模板
     * @return [type] [description]
     */
    public function student_grade_add(){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();

        echo $this->load->view('admin/student_grade_add',array('base'=>$base,'academy_list'=>$academy_list));
    }
    /**
     * 教师教的学生的信息，添加成绩
     * @return [type] [description]
     */
    public function student_grade_panel($page="1-6.html"){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');
        $this->load->library('common');

        $success=true;
        $msg='';

        $pageNumber=$this->common->getPage($page);

        $str['academy']=@$_POST['academy']?@$_POST['academy']:'';
        $str['major']=@$_POST['major']?@$_POST['major']:'';
        $str['period']=@$_POST['period']?@$_POST['period']:'';
        $semester=@$_POST['semester']?@$_POST['semester']:'';

        $t_id=$_SESSION['id'];
        $this->load->model('m_teacher','teacher',true);
        $teacher_info=$this->teacher->teacher_search(array('t_id'=>$t_id));
        $c_id=$teacher_info[0]->c_id;

        $this->load->model('m_course','course',true);
        if($this->course->course_plan_search(array('academy'=>$str['academy'],'major'=>$str['major'],'period'=>$str['period'],'semester'=>$semester,'t_id'=>$t_id,'c_id'=>$c_id),'1')){
            $this->load->model('m_student','student',true);
            $result=$this->student->student_search($str);

            foreach ($result as $key => $value) {
                $results=$this->student->student_transcript_search(array('s_id'=>$value->s_id,'semester'=>$semester));
                if($results){
                    $value->grade=$results[0]->grade;
                }
            }
            $pageInfo=$this->common->pageInfo($pageNumber[0],$pageNumber[1],$result);

            $panel=$this->load->view('admin/student_grade_panel',array('base'=>$base,'pageInfo'=>$pageInfo,'semester'=>$semester),true);
        }else{
            $success=false;
            $panel='';
            $msg="没有课程安排！";
        }
        echo json_encode(array('success'=>$success,'panel'=>$panel,'msg'=>$msg));
    }
    public function update_transcript($param){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');
        $this->load->library('common');

        $msg='';
        $success=true;
        $s_id=$param;
        $t_id=$_SESSION['id'];

        $this->load->model('m_teacher','teacher',true);
        $teacher_info=$this->teacher->teacher_search(array('t_id'=>$t_id));
        $c_id=$teacher_info[0]->c_id;

        $semester=@$_POST['semester']?@$_POST['semester']:'';
        $grade=@$_POST['grade']?@$_POST['grade']:'';
        if($grade){
            $grade=intval($grade);
            if($grade<=100 && $grade>=0){
                if($grade<60){
                    $state=2;
                }else{
                    $state=1;
                }
            }else{
                $success=false;
                $msg="请填写有效分数！";
            }
        }else{
            $success=false;
            $msg="请填写分数！";
        }
        if($success){
            $this->load->model('m_student','student',true);
            if($this->student->student_transcript_search(array('s_id'=>$s_id,'c_id'=>$c_id,'semester'=>$semester))){
                if($this->student->student_transcript_update(array('grade'=>$grade,'state'=>$state),array('s_id'=>$s_id,'c_id'=>$c_id,'semester'=>$semester))){
                    $msg='更新成功！';
                }else{
                    $success=false;
                    $msg="更新失败！";
                }
            }else{
                if($this->student->student_transcript_insert(array('s_id'=>$s_id,'c_id'=>$c_id,'semester'=>$semester,'grade'=>$grade,'state'=>$state))){
                    $msg="添加成功！";
                }else{
                    $msg="添加失败！";
                    $success=false;
                }
            }
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
    /**
     * 教师选择选修课
     * @return [type] [description]
     */
    public function teacher_elective_panel(){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        echo $this->load->view('admin/teacher_elective_panel',array('base'=>$base));
    }
    public function elective_course_list($params="1-6.html"){
        session_start();
        $success=true;
        $msg='';

        $this->load->helper('url');
        $base=$this->config->item('base_url');
        $this->load->library('common');
        $page=$this->common->getPage($params);

        $str['year']=@$_POST['year']?@$_POST['year']:'';

        $this->load->model('m_course','course',true);
        $result=$this->course->course_plan_search($str,'2');

        $weekday[1]="星期一";
        $weekday[2]="星期二";
        $weekday[3]="星期三";
        $weekday[4]="星期四";
        $weekday[5]="星期五";
        $weekday[6]="星期六";
        $weekday[7]="星期日";

        $section[1]="第一节";
        $section[2]="第二节";
        $section[3]="第三节";
        $section[4]="第四节";
        $section[5]="第五节";

        if($result){
            foreach ($result as $key => $value) {
                $course=$this->course->course_base_info($value->c_id);
                if($course){
                    $value->course=$course[0]->name;
                }else{
                    $value->course='无';
                }
                $value->weekday=$weekday[$value->weekday];
                $value->section=$section[$value->section];
            }
            $pageInfo=$this->common->pageInfo($page[0],$page[1],$result);
            $panel=$this->load->view("admin/teacher_elective_list",array("base"=>$base,"pageInfo"=>$pageInfo),true);
        }else{
            $success=false;
            $msg="没有选修课程！";
            $panel='';
        }
        echo json_encode(array("success"=>$success,"msg"=>$msg,"panel"=>$panel));
    }
    /**
     * 教师选择选修课
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function teacher_elective_add($param){
        session_start();
        $success=true;
        $msg='';

        $this->load->helper('url');
        $base=$this->config->item('base_url');

        if($_SESSION['classify']=='1'){
            $this->load->model('m_course','course',true);
            if($this->course->course_plan_update(array('t_id'=>$_SESSION['id']),array('id'=>$param))){
                $msg="选择成功！";
            }else{
                $msg="选择失败！";
            }
        }else{
            $success=false;
            $msg="非教师不能任教选修课！";
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
    /**
     * 取消教师选择的选修
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function teacher_elective_delete($param){
        session_start();
        $success=true;
        $msg='';

        $this->load->helper('url');
        $base=$this->config->item('base_url');

        if($_SESSION['classify']=='1'){
            $this->load->model('m_course','course',true);
            if($this->course->course_plan_update(array('t_id'=>'-1'),array('id'=>$param))){
                $msg="取消选择成功！";
            }else{
                $msg="取消选择失败！";
            }
        }else{
            $success=false;
            $msg="非教师不能操作！";
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
}